package io.renren.modules.backstage.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.backstage.service.VideoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.backstage.entity.ChapterEntity;
import io.renren.modules.backstage.service.ChapterService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-04-25 13:39:07
 */
@RestController
@RequestMapping("backstage/chapter")
public class ChapterController {
    @Resource
    private ChapterService chapterService;
    @Resource
    private VideoService videoService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("backstage:chapter:list")
    public R list(@RequestParam Map<String, Object> params){
        String id = (String) params.get("id");
        if(id!=null&& !id.equals("")){
            PageUtils page= chapterService.queryPage1(params);
            return R.ok().put("page", page);
        }
        PageUtils page  = chapterService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 课程章节信息
     */
    @RequestMapping("/info/{cid}")
    @RequiresPermissions("backstage:chapter:info")
    public R info(@PathVariable("cid") Integer cid){
		ChapterEntity chapter = chapterService.getById(cid);
        return R.ok().put("chapter", chapter);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("backstage:chapter:save")
    public R save(@RequestBody ChapterEntity chapter){
		chapterService.save(chapter);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("backstage:chapter:update")
    public R update(@RequestBody ChapterEntity chapter){
		chapterService.updateById(chapter);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("backstage:chapter:delete")
    public R delete(@RequestBody Integer[] cids){
		chapterService.removeByIds(Arrays.asList(cids));
        //根据章节ID删除多应的视频
        for (Integer cid : cids) {
            videoService.deleteByCid(cid);
        }
        return R.ok();
    }


}
